package com.wang.mybatis.binding;

import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * 创建代理对象工厂
 * @param <T>
 */
public class MapperProxyFactory <T> {
    private final Class<T> mapperInterface; // 接口类型

    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    @SuppressWarnings("unchecked")
    // 生成代理对象
    public T newInstance(Map<String,String> sqlSession) {
        MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
    }
}
